package com.sfh.cp.mass.pst.impl;

import android.text.TextUtils;

import com.sfh.cp.mass.ReqAddMassContent;
import com.sfh.cp.mass.pst.IPstMassAddContent;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvp.service.AbstractPresenter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadBatchListener;
import crm.sfh.server.Const;
import crm.sfh.server.MangerUser;
import crm.sfh.server.SMass;
import crm.sfh.server.SNote;
import crm.sfh.server.SUser;
import crm.sfh.server.entity.EMass;

/**
 * 功能描述:
 *
 * @date 2018/5/2
 */
public class PstMassAddContent extends AbstractPresenter<IPstMassAddContent.IVAddMassContent> implements IPstMassAddContent {
    @Override
    public void updateFile(List<String> file) {

        final String[] filePaths = file.toArray(new String[file.size()]);
        BmobFile.uploadBatch(AppCacheManager.getApplication(), filePaths, new UploadBatchListener() {
            @Override
            public void onSuccess(List<BmobFile> files, List<String> urls) {
                //1、files-上传完成后的BmobFile集合，是为了方便大家对其上传后的数据进行操作，例如你可以将该文件保存到表中
                //2、urls-上传文件的完整url地址
                getView().onFileSuccess(urls);
            }

            @Override
            public void onProgress(int i, int i1, int i2, int i3) {

            }

            @Override
            public void onError(int i, String s) {

            }
        });

    }

    @Override
    public void add(final ReqAddMassContent req) {
        if (TextUtils.isEmpty(req.getMassId())) {
            getView().showToast("请选择社区");
            return;
        }
        getView().showLoading(false);
        final SUser user = AppCacheManager.newInstance().getCache(Const.USER, new SUser());
        SNote note = new SNote();
        note.setLable(req.getLable());
        note.setMassId(req.getMassId());
        note.setTitle(req.getTitle());
        note.setAuthor(user);
        if (req.getUrls() == null || req.getUrls().isEmpty()) {
            note.setType(0);
        } else {
            note.setType(1);
            StringBuffer buffer = new StringBuffer(50);
            for (String url : req.getUrls()) {
                buffer.append(url).append(";");
            }
            note.setContent(buffer.substring(0, buffer.length() - 1).toString());
        }
        note.setCity(TextUtils.isEmpty(user.getAddress()) ? "" : user.getAddress());
        note.setSizeFabulous(0L);
        note.setSizeMessage(0L);
        note.setSizeReading(0L);
        note.save(AppCacheManager.getApplication(), new SaveListener() {
            @Override
            public void onSuccess() {
                getView().hideLoading();
                getView().onAddSuccess();
                SMass mass = new SMass();
                mass.setObjectId(req.getMassId());
                mass.increment("size", 1);
                mass.update(AppCacheManager.getApplication());
                new MangerUser().updateCharm(AppCacheManager.getApplication(), user.getObjectId(), 10, null);
            }

            @Override
            public void onFailure(int i, String s) {
                getView().hideLoading();
                getView().showToast(s);
            }
        });
    }

    @Override
    public void listMass() {
        getView().showLoading(true);
        BmobQuery<SMass> query = new BmobQuery<>();
        query.findObjects(AppCacheManager.getApplication(), new FindListener<SMass>() {
            @Override
            public void onSuccess(List<SMass> list) {
                getView().hideLoading();
                List<EMass> masses = new ArrayList<>(list.size());
                for(SMass mass:list){
                    masses.add(mass.getEMass());
                }
                getView().onListMass(masses);
            }

            @Override
            public void onError(int i, String s) {
                getView().hideLoading();
                getView().showToast(s);
            }
        });
    }

}
